package com.example.java.controller;

import com.example.java.pojo.ResponseMessage;
import com.example.java.pojo.User;
import com.example.java.pojo.UserAddress;
import com.example.java.pojo.dto.UserDto;
import com.example.java.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController    //接口方法返回的对象，转换成json文本
@RequestMapping("/user")    // localhost:8080/user/****
public class UserController {

    @Autowired
    IUserService userService;

    //注册用户
    @PostMapping("/registeruser")
    public ResponseMessage<User> registerUser(@Validated @RequestBody UserDto user){
        User userNew = userService.registerUser(user);//业务中注册一个新用户
        return ResponseMessage.success(userNew);
    }
    //用户登录
    @GetMapping("/loginuser")
    public ResponseMessage<User> loginUser(@Validated @RequestBody UserDto user){
        User userNew = userService.loginUser(user);//业务中用户登录
        return ResponseMessage.success(userNew);
    }
    //查询用户所有信息
    @GetMapping("/getuserinfo")
    public ResponseMessage<User> getUserinfo(@Validated @RequestBody UserDto user){
        User userNew = userService.getUserinfo(user);//业务中查询用户所有信息
        return ResponseMessage.success(userNew);
    }
    //删除一个用户
    @DeleteMapping("/{id}")
    public ResponseMessage<User> deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);//业务中根据id来删除一个用户
        return ResponseMessage.success();
    }
    //添加一个账号
    @PostMapping("/adduser")
    public ResponseMessage<User> addUser(@Validated @RequestBody UserDto user){
        User userNew = userService.addUser(user);//业务中添加一个账号
        return ResponseMessage.success(userNew);
    }
    //禁用一个账号
    @PutMapping("/banuser")
    public ResponseMessage<User> banUser(@Validated @RequestBody UserDto user){
        User userNew = userService.banUser(user);//业务中将一个账号禁用
        return ResponseMessage.success(userNew);
    }
    //修改一个账号信息
    @PutMapping("/edituser")
    public ResponseMessage<User> editUser(@Validated @RequestBody UserDto user){
        User userNew = userService.editUser(user);//业务中的修改用户信息
        return ResponseMessage.success(userNew);
    }

    //查询一个账户所有的有效地址
    @GetMapping("/getuseralladdresses")
    public ResponseMessage<List<UserAddress>> getUserAllAddresses(@Validated @RequestBody UserDto user){
        List<UserAddress> userNew = userService.getUserAllAddresses(user);//业务中查询一个账户所有的有效地址
        return ResponseMessage.success(userNew);
    }

    //根据用户id去查询
    @GetMapping("/{id}")
    public ResponseMessage<User> getUser(@PathVariable Integer id){
        User userNew = userService.getUser(id);//业务中根据id查询用户
        return ResponseMessage.success(userNew);
    }


}
